#!/bin/bash

###
# @author       Thomas Fricke <thf.swd@googlemail.com>
# @date         2013-05-14 15:14h
# @desc         This is (or will become) a Script that makes switching TV channels 
#               easy. To use it without modification, you have to meet the following 
#               prerequisites:  + setup and connected DVB-C card
#                                       + dvblast installed
#                                       + KabelBW (de) as your TV provider (otherwise the 
#                                         frequencies and/or names won’t work)
#                                       + have the bundle of config files in subfolder 
#                                         ‘config’ (part of the package)
# @todo         + include a search function in case the channels list gets too long
#               + include switch for:
#                       - Free TV
#                       - Pay TV
#                       - Radio
#     + ask user for IP address and port to stream to (offering a default value 
#       stored in config file)
#               + make this program a full binary using shc
#               + check if dvblast is installed and if not, do it
#               + maybe autofetching of a channels list would be nice?
###
echo ""
echo "Please enter the IP address you want to and the port."
echo "If you want to keep the default values, just press enter at both prompts."
echo "Default IP:PORT is 192.168.1.60:1234"
echo ""

read -p "IP   : " IP
read -p "Port : " PORT

if [[ ! $IP ]]
then
  IP=192.168.1.60
fi

if [[ ! $PORT ]]
then
  PORT=1234
fi
echo "Streaming address set to " $IP":"$PORT
echo "Select a channel!"

# define channels array
declare -a channels
TMPFILE=config/temp.conf

# awk 'NR % 3 == 0'  input-file >output-file

# set channel names to select from
channels[${#channels[*]}]="13th Street";  
channels[${#channels[*]}]="Animal Planet"; 
channels[${#channels[*]}]="Animax"; 
channels[${#channels[*]}]="AXN Action"; 
channels[${#channels[*]}]="Biography Channel";    
channels[${#channels[*]}]="Fox";
channels[${#channels[*]}]="History";
channels[${#channels[*]}]="Kabel Eins Classics";
channels[${#channels[*]}]="Kinowelt TV";
channels[${#channels[*]}]="National Geographic";
channels[${#channels[*]}]="Pro 7";
channels[${#channels[*]}]="RTL Living";
channels[${#channels[*]}]="Sat.1 Emotions";
channels[${#channels[*]}]="SyFy";
channels[${#channels[*]}]="Tele 5";
channels[${#channels[*]}]="TNT Film";
channels[${#channels[*]}]="TNT Serie";
channels[${#channels[*]}]="YFE";
channels[${#channels[*]}]="quit";

# run command matching the selection
select chan in "${channels[@]}"; do
  case ${chan} in
    # @ToDo: implement function which automatically creates a config file 
    #        with the needed parameters and deletes it when done
    ${channels[0]}) echo "$IP:$PORT  1 42" > $TMPFILE; dvblast -m qam_256 -f 402000000 -s 6900000 -C -c $TMPFILE; ;;      # 13th street
    ${channels[1]}) echo "$IP:$PORT  1 19116" > $TMPFILE; dvblast -m qam_256 -f 450000000 -s 6900000 -C -c $TMPFILE; ;;   # animal planet
    ${channels[2]}) echo "$IP:$PORT  1 25105" > $TMPFILE; dvblast -m qam_256 -f 418000000 -s 6900000 -C -c $TMPFILE; ;;   # animax
    ${channels[3]}) echo "$IP:$PORT  1 13109" > $TMPFILE; dvblast -m qam_256 -f 418000000 -s 6900000 -C -c $TMPFILE; ;;   # axn action
    ${channels[4]}) echo "$IP:$PORT  1 25104" > $TMPFILE; dvblast -m qam_256 -f 418000000 -s 6900000 -C -c $TMPFILE; ;;   # biography channel
    ${channels[5]}) echo "$IP:$PORT  1 1016" > $TMPFILE; dvblast -m qam_256 -f 410000000 -s 6900000 -C -c $TMPFILE; ;;    # fox
    ${channels[6]}) echo "$IP:$PORT  1 13102" > $TMPFILE; dvblast -m qam_256 -f 418000000 -s 6900000 -C -c $TMPFILE; ;;   # history
    ${channels[7]}) echo "$IP:$PORT  1 13112" > $TMPFILE; dvblast -m qam_256 -f 418000000 -s 6900000 -C -c $TMPFILE; ;;   # kabel eins classics
    ${channels[8]}) echo "$IP:$PORT  1 13107" > $TMPFILE; dvblast -m qam_256 -f 418000000 -s 6900000 -C -c $TMPFILE; ;;   # kinowelt tv
    ${channels[9]}) echo "$IP:$PORT  1 13" > $TMPFILE; dvblast -m qam_256 -f 402000000 -s 6900000 -C -c $TMPFILE; ;;      # national geographic
    ${channels[10]}) echo "$IP:$PORT  1 12103" > $TMPFILE; dvblast -m qam_256 -f 114000000 -s 6900000 -C -c $TMPFILE; ;;   # pro 7
    ${channels[11]}) echo "$IP:$PORT  1 11109" > $TMPFILE; dvblast -m qam_256 -f 450000000 -s 6900000 -C -c $TMPFILE; ;;   # rtl living
    ${channels[12]}) echo "$IP:$PORT  1 13113" > $TMPFILE; dvblast -m qam_256 -f 418000000 -s 6900000 -C -c $TMPFILE; ;;   # sat.1 emotions
    ${channels[13]}) echo "$IP:$PORT  1 36" > $TMPFILE; dvblast -m qam_256 -f 410000000 -s 6900000 -C -c $TMPFILE; ;;    # syfy
    ${channels[14]}) echo "$IP:$PORT  1 12111" > $TMPFILE; dvblast -m qam_256 -f 450000000 -s 6900000 -C -c $TMPFILE; ;;   # tele 5
    ${channels[15]}) echo "$IP:$PORT  1 14111" > $TMPFILE; dvblast -m qam_256 -f 458000000 -s 6900000 -C -c $TMPFILE; ;;   # tnt film
    ${channels[16]}) echo "$IP:$PORT  1 50" > $TMPFILE; dvblast -m qam_256 -f 410000000 -s 6900000 -C -c $TMPFILE; ;;      # tnt serie
    ${channels[17]}) echo "$IP:$PORT  1 21101" > $TMPFILE; dvblast -m qam_256 -f 546000000 -s 6900000 -C -c $TMPFILE; ;;   # yourfamilyentertainment
    (quit) break; ;;
    (*) echo "${chan}"; ;;
  esac
done
